package com.fieldchain.ex.fa100.utils.chart.auxiliary;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/8/27
 */

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;

import com.fieldchain.ex.fa100.utils.chart.dataset.MyBarDataSet;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.buffer.BarBuffer;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.renderer.BarChartRenderer;
import com.github.mikephil.charting.utils.MPPointD;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DecimalFormat;

/**
 * Created by loro on 2017/2/8.
 */
public class MyBarChartRenderer extends BarChartRenderer {
    protected float barOffset;//BarChart绘制时偏移多少个单位 --小于0时向左偏移
    protected float highlightWidth, highlightSize;//高亮线宽度 单位:dp  /  高亮文字大小 单位:px
    private RectF mBarShadowRectBuffer = new RectF();
    private DecimalFormat format = new DecimalFormat("0.00");//保留小数点后四位
    public MyBarChartRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
    }

    @Override
    public void drawData(Canvas c) {
        BarData barData = mChart.getBarData();
        for (int i = 0; i < barData.getDataSetCount(); i++) {
            MyBarDataSet set = (MyBarDataSet) barData.getDataSetByIndex(i);
            Log.e("mRenderPaint1",mRenderPaint.getColor()+"");
            if (set.isVisible() && set.getEntryCount() > 0) {
                Log.e("mRenderPaint1",mRenderPaint.getColor()+"");
                drawDataSet(c, set, i);
            }
        }
    }

    protected void drawDataSet(Canvas c, MyBarDataSet dataSet, int index) {

        Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
        mBarBorderPaint.setColor(dataSet.getBarBorderColor());
        mBarBorderPaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getBarBorderWidth()));

        final boolean drawBorder = dataSet.getBarBorderWidth() > 0.f;

        float phaseX = mAnimator.getPhaseX();
        float phaseY = mAnimator.getPhaseY();


        // initialize the buffer
        BarBuffer buffer = mBarBuffers[index];
        buffer.setPhases(phaseX, phaseY);
        buffer.setDataSet(index);
        buffer.setInverted(mChart.isInverted(dataSet.getAxisDependency()));
        buffer.setBarWidth(mChart.getBarData().getBarWidth());

        buffer.feed(dataSet);

        trans.pointValuesToPixel(buffer.buffer);

        final boolean isSingleColor = dataSet.getColors().size() == 1;

        if (isSingleColor) {
            mRenderPaint.setColor(dataSet.getColor());
        }

        for (int j = 0; j < buffer.size(); j += 4) {
            //源码修改处----------------------------------------------------------------------------------------------
            int i = j / 4;
            if (dataSet.getEntryForIndex(i).getClose() < dataSet.getEntryForIndex(i).getOpen()) {
                mRenderPaint.setColor(dataSet.getColors().get(1));
                mRenderPaint.setStyle(Paint.Style.FILL);
            } else {
                mRenderPaint.setColor(dataSet.getColors().get(0));
                mRenderPaint.setStyle(Paint.Style.FILL);
            }

            Log.e("mRenderPaint",mRenderPaint.getColor()+"");
            if (!mViewPortHandler.isInBoundsLeft(buffer.buffer[j + 2]))
                continue;

            if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
                break;

            c.drawRect(buffer.buffer[j]+20, buffer.buffer[j + 1], buffer.buffer[j + 2]+20, buffer.buffer[j + 3], mRenderPaint);

            if (drawBorder) {
                c.drawRect(buffer.buffer[j], buffer.buffer[j + 1], buffer.buffer[j + 2],
                        buffer.buffer[j + 3], mBarBorderPaint);
            }
        }
    }
    @Override
    public void drawHighlighted(Canvas c, Highlight[] indices) {
        BarData barData = mChart.getBarData();
        for (Highlight high : indices) {
            IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
            if (set == null || !set.isHighlightEnabled())
                continue;
            BarEntry e = set.getEntryForXValue(high.getX(), high.getY());
            if (!isInBoundsX(e, set))
                continue;

            mHighlightPaint.setColor(set.getHighLightColor());
            mHighlightPaint.setStrokeWidth(highlightWidth);
            mHighlightPaint.setTextSize(highlightSize);

            //画竖线
            float barWidth = barData.getBarWidth();
            Transformer trans = mChart.getTransformer(set.getAxisDependency());
            prepareBarHighlight(e.getX() + barOffset, 0, 0, barWidth / 2, trans);

            float xp = mBarRect.centerX();
            c.drawLine(xp, mViewPortHandler.getContentRect().bottom, xp, 0, mHighlightPaint);

            //判断是否画横线
            float y = high.getDrawY();
            float yMaxValue = mChart.getYChartMax();
            float yMin = getYPixelForValues(xp, yMaxValue);
            float yMax = getYPixelForValues(xp, 0);
            if (y >= 0 && y <= yMax) {//在区域内即绘制横线
                float xMax = mChart.getWidth();
                int halfPaddingVer = 5;//竖向半个padding
                int halfPaddingHor = 8;
                //先绘制文字框
                mHighlightPaint.setStyle(Paint.Style.STROKE);
                float yValue = (yMax - y) / (yMax - yMin) * yMaxValue;
                String text = format.format(yValue);
                int width = Utils.calcTextWidth(mHighlightPaint, text);
                int height = Utils.calcTextHeight(mHighlightPaint, text);
                float top = Math.max(0, y - height / 2F - halfPaddingVer);//考虑间隙
                float bottom = top + height + halfPaddingVer * 2;
                if (bottom > yMax) {
                    bottom = yMax;
                    top = bottom - height - halfPaddingVer * 2;
                }
                RectF rect = new RectF(xMax - width - halfPaddingHor * 2, top, xMax, bottom);
                c.drawRect(rect, mHighlightPaint);
                //再绘制文字
                mHighlightPaint.setStyle(Paint.Style.FILL);
                Paint.FontMetrics metrics = mHighlightPaint.getFontMetrics();
                float baseY = (top + bottom - metrics.top - metrics.bottom) / 2;
                c.drawText(text, xMax - width - halfPaddingHor, baseY, mHighlightPaint);
                //绘制横线
                c.drawLine(0, y, xMax - width - halfPaddingHor * 2, y, mHighlightPaint);
            }
        }
    }

    protected float getYPixelForValues(float x, float y) {
        MPPointD pixels = mChart.getTransformer(YAxis.AxisDependency.LEFT).getPixelForValues(x, y);
        return (float) pixels.y;
    }
}

